package com.runen.infinitemovement.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.runen.infinitemovement.Constant;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.ShopAdapter;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.ChallengeAPI;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.base.BaseResponseEntity;
import com.runen.infinitemovement.model.entity.EventBusEntity;
import com.runen.infinitemovement.model.entity.HozomSurplusEntity;
import com.runen.infinitemovement.model.entity.ShopInfoEntity;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.tool.notchtool.NotchTools;
import com.runen.infinitemovement.tool.notchtool.core.NotchProperty;
import com.runen.infinitemovement.tool.notchtool.core.OnNotchCallBack;
import com.runen.infinitemovement.widget.popupwind.HozomPoppupWindow;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyHozomActivity extends BaseActivity {
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.tv_hozom_detail)
    TextView tvHozomDetail;
    @BindView(R.id.btn_finish)
    Button btnFinish;
    @BindView(R.id.rv_shop)
    RecyclerView rvShop;
    @BindView(R.id.tv_hozom_num)
    TextView tvHozomNum;
    @BindView(R.id.tv_gx_num)
    TextView tvGxNum;
    @BindView(R.id.tv_team_progress_value)
    TextView tvTeamProgressValue;
    @BindView(R.id.tv_see_progress)
    TextView tvSeeProgress;
    @BindView(R.id.tv_challenge)
    TextView tvChallenge;
    @BindView(R.id.tv_day_task)
    TextView tvDayTask;
    @BindView(R.id.ll_go_contribute)
    LinearLayout llGoContribute;
    @BindView(R.id.tv_shop_more)
    TextView tvShopMore;
    private ShopAdapter shopAdapter;
    private HozomSurplusEntity.DataBean dataBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_hozom;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        int height = CommonUtil.getStatusBarHeight(this);
//        rlTitle.setPadding(0, height + 10, 0, 40);
        //适配刘海屏
        NotchTools.getFullScreenTools().translucentStatusBar(this, new OnNotchCallBack() {
            @Override
            public void onNotchPropertyCallback(NotchProperty notchProperty) {
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) rlTitle.getLayoutParams();
                layoutParams.topMargin = height;
                rlTitle.setLayoutParams(layoutParams);
            }
        });

        LinearLayoutManager layoutManager = new GridLayoutManager(this, 3);
        rvShop.setLayoutManager(layoutManager);
        shopAdapter = new ShopAdapter(this);
        rvShop.setAdapter(shopAdapter);
        initData();
        initShop();

        shopAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Intent intent1 = new Intent(MyHozomActivity.this, WebActivity.class);
                intent1.putExtra("LINK_URL", String.format(Constant.SHOP_DETAILS_H5,
                        UserAPI.getUserToken(), shopAdapter.getData().get(position).id));
                startActivity(intent1);
            }
        });
    }

    private void initShop() {
        String str = "?limit=3&page=1";
        new UserAPI().hzomShop(str, new BaseResultCallback<ShopInfoEntity>() {
            @Override
            public void onResponse(ShopInfoEntity response) {
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        shopAdapter.setNewInstance(response.data);
                    }
                }
            }
        });
    }

    private void initData() {
        showDialog();
        new UserAPI().hozomSurplus(new BaseResultCallback<HozomSurplusEntity>() {
            @Override
            public void onResponse(HozomSurplusEntity response) {
                dismissDialog();
                if (response.code == 0) {
                    if (response.data != null) {
                        dataBean = response.data;
                        tvHozomNum.setText(String.valueOf(dataBean.surplus));
                        tvGxNum.setText(String.valueOf(dataBean.used + "颗"));
                        tvTeamProgressValue.setText(String.valueOf(dataBean.progress));
                    }
                }
            }
        });
    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }

    @OnClick({R.id.btn_finish, R.id.tv_hozom_detail, R.id.tv_see_progress, R.id.tv_challenge, R.id.tv_day_task,
            R.id.ll_go_contribute, R.id.tv_shop_more})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_hozom_detail:
                startActivity(new Intent(this, HozomDetailActivity.class));
                break;
            case R.id.btn_finish:
                finish();
                break;
            case R.id.tv_see_progress:
//                Intent intent = new Intent(this, MyTheLongMarchRoadActivity.class);
//                intent.putExtra("userId", UserAPI.getUserId());
//                startActivity(intent);
                EventBus.getDefault().post(new EventBusEntity(EventBusEntity.CODE_109));
                EventBus.getDefault().post(new EventBusEntity(EventBusEntity.CODE_100));
                finish();
                break;
            case R.id.tv_challenge:
                Intent activityH5 = new Intent(this, WebActivity.class);
                activityH5.putExtra("LINK_URL", String.format(Constant.ACTIVITY_H5, UserAPI.getUserToken()));
                startActivity(activityH5);
                break;
            case R.id.tv_day_task:
                startActivity(new Intent(this, DayTaskActivity.class));
                break;
            case R.id.ll_go_contribute:
                showHozom();
                break;
            case R.id.tv_shop_more:
                Intent intent1 = new Intent(this, WebActivity.class);
                intent1.putExtra("LINK_URL", String.format(Constant.HOZOM_SHOP_H5, UserAPI.getUserToken()));
                startActivity(intent1);
                break;
        }
    }

    /**
     * 贡献火种
     */
    private void showHozom() {
        HozomPoppupWindow hozomPoppupWindow = HozomPoppupWindow.getInstance(this);
        hozomPoppupWindow.show();
        hozomPoppupWindow.setOnHozonClickListener(new HozomPoppupWindow.OnHozonClickListener() {
            @Override
            public void onHozomClick(int number) {
                if (dataBean == null) {
                    return;
                }
                if (dataBean.surplus < number) {
                    ToastUtils.showShort("火种数量不足" + number + "颗");
                } else {
                    JSONObject object = new JSONObject();
                    try {
                        object.put("tinder", number);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    showDialog();
                    new ChallengeAPI().onHozomTinder(object.toString(), new BaseResultCallback<BaseResponseEntity<?>>() {
                        @Override
                        public void onResponse(BaseResponseEntity<?> response) {
                            dismissDialog();
                            hozomPoppupWindow.dismiss();
                            if (response.getCode() == 0) {
                                dataBean.surplus = dataBean.surplus - number;
                                tvHozomNum.setText(String.valueOf(dataBean.surplus));
                                dataBean.used = dataBean.used + number;
                                tvGxNum.setText(dataBean.used + "颗");
                            } else {
                                ToastUtils.showShort(response.getMessage());
                            }
                        }
                    });
                }
            }
        });
    }
}